package ru.spbstu.mvp.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import ru.spbstu.mvp.entity.Token;

import java.util.List;
import java.util.Optional;

@Repository
public interface TokenRepository extends JpaRepository<Token, Integer> {

    @Query(
            value = """
                    select t from Token t inner join User u\s
                    on t.user.id = u.id\s
                    where u.id = :id and (t.expired = false or t.revoked = false)\s
                    """
    )
    List<Token> findAllValidTokenByUser(Integer id);


    @Query("DELETE FROM Token t WHERE t.expired = true")
    void deleteExpiredTokens();

    Optional<Token> findByToken(String token);
}
